<?php

namespace App\Console\Commands\Plane\Rules;

use App\Models\Plane\Rules\PlaneLuggageRule;
use Illuminate\Console\Command;
use Maatwebsite\Excel\Facades\Excel;

class PlaneLuggageRuleImport extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'plane.luggage:import';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '机场行李规则导入';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $excel = Excel::toArray(collect(), storage_path('plane/seeds/luggage_rules.xlsx'));
        $data  = $excel[0];
        unset($data[0]);
        $rules                      = $data;
        $column_company_name        = 0; // 航空公司
        $column_carrier_code        = 1; // 航司二字码
        $column_cabin_name          = 4; // 注意 无行李特惠
        $column_cabin_code          = 5; // 舱位码
        $column_type                = 3; // 区分方式
        $column_checked_weight      = 7; // 托运行李
        $column_checked_volume      = 8; // 托运行李
        $column_checked_number      = 9; // 托运行李
        $column_carry_weight        = 11;// 手提行李
        $column_carry_volume        = 12;// 手提行李
        $column_carry_number        = 13;// 手提行李
        $column_carry_single_weight = -1;
        $column_baby_checked_weight = -1;
        $column_baby_checked_volume = -1;
        $column_baby_checked_number = -1;
        $column_baby_desc           = 14;            // 婴儿描述
        $column_baby_desc_uy        = 15;            //
        $column_remark              = 16;            // 备注
        $column_remark_uy           = 17;            //
        $column_default             = 19;            // 是否默认
        $from                       = 1;
        $types                      = [
            "舱位区分" => 1,
            "票价区分" => 2,
        ];

        $last_carrier_code = ""; // 航司二字码
        $last_type         = ""; // 区分类型
        $last_cabin_name   = ""; // 舱位名称
        $last_company_name = ""; //  名称
        $last_default      = "";
        $last_baby_desc    = "";
        $last_baby_desc_uy = "";
        $last_remark       = "";
        $last_remark_uy    = "";
        $ruleRepo          = new PlaneLuggageRule();
        foreach ($rules as $key => $rule) {
            // 验证是否有
            if (!$rule[$column_carrier_code]) {
                $carrier_code = $last_carrier_code;
            } else {
                $carrier_code      = $rule[$column_carrier_code];
                $last_carrier_code = $carrier_code;
            }

            // 跳过航班 九元 和 春秋
            if (in_array($carrier_code, ['9C', 'AQ'])) {
                continue;
            }

            // 获取区分方式
            if (!$rule[$column_type]) {
                $type = $last_type;
            } else {
                $type      = $rule[$column_type];
                $last_type = $type;
            }

            // 获取区分方式
            if (!$rule[$column_default]) {
                $default = $last_default;
            } else {
                $default      = $rule[$column_default];
                $last_default = $default;
            }

            // 获取
            if (!$rule[$column_baby_desc]) {
                $baby_desc = $last_baby_desc;
            } else {
                $baby_desc      = $rule[$column_baby_desc];
                $last_baby_desc = $baby_desc;
            }

            // 获取
            if (!$rule[$column_baby_desc_uy]) {
                $baby_desc_uy = $last_baby_desc_uy;
            } else {
                $baby_desc_uy      = $rule[$column_baby_desc_uy];
                $last_baby_desc_uy = $baby_desc_uy;
            }

            // 获取
            if (!$rule[$column_remark]) {
                $remark = $last_remark;
            } else {
                $remark      = $rule[$column_remark];
                $last_remark = $remark;
            }

            // 获取
            if (!$rule[$column_remark_uy]) {
                $remark_uy = $last_remark_uy;
            } else {
                $remark_uy      = $rule[$column_remark_uy];
                $last_remark_uy = $remark_uy;
            }


            if (!$rule[$column_company_name]) {
                $company_name = $last_company_name;
            } else {
                $company_name      = $rule[$column_company_name];
                $last_company_name = $company_name;
            }


            // 验证是否有
            if (!$rule[$column_cabin_name]) {
                $cabin_name = $last_cabin_name;
            } else {
                $cabin_name      = $rule[$column_cabin_name];
                $last_cabin_name = $cabin_name;
            }

            $checked_weight = $rule[$column_checked_weight] ?: '';
            $checked_volume = $rule[$column_checked_volume] ?: '';
            $checked_number = $rule[$column_checked_number] ?: '';
            $carry_weight   = $rule[$column_carry_weight] ?: '';
            $carry_volume   = $rule[$column_carry_volume] ?: '';
            $carry_number   = $rule[$column_carry_number] ?: '';

            $base_info = [
                "carrier_code"        => $carrier_code,
                "cabin_name"          => $cabin_name,
                "company_name"        => $company_name,
                "checked_weight"      => $checked_weight,
                "checked_volume"      => $checked_volume,
                "checked_number"      => $checked_number,
                "carry_weight"        => $carry_weight,
                "carry_volume"        => $carry_volume,
                "carry_number"        => $carry_number,
                "carry_single_weight" => "",
                "baby_checked_weight" => "",
                "baby_checked_volume" => "",
                "baby_checked_number" => "",
                "baby_desc"           => $baby_desc,
                "baby_desc_uy"        => $baby_desc_uy,
                "remark"              => $remark,
                "remark_uy"           => $remark_uy,
                "from"                => $from,
                "type"                => $types[$type],
                "default"             => $default == '是' ? 1 : 0,
                "ticket_price_scopes" => "",
            ];
            $cabin_str = $rule[$column_cabin_code];
            if ($cabin_str == '除下述仓位') {
                $base_info['cabin_code'] = '';
                $ruleRepo->add($base_info);

            } elseif ($cabin_str == '全价') {
                $base_info['cabin_code']          = '';
                $base_info['ticket_price_scopes'] = '0,1';
                $ruleRepo->add($base_info);

            } elseif ($cabin_str == '全价-5折') {
                $base_info['cabin_code']          = '';
                $base_info['ticket_price_scopes'] = '0.5,0.999';
                $ruleRepo->add($base_info);

            } elseif ('5折以下' == $cabin_str) {
                $base_info['cabin_code']          = '';
                $base_info['ticket_price_scopes'] = '0,0.499';
                $ruleRepo->add($base_info);

            } elseif (is_null($cabin_str)) {
                $base_info['cabin_code'] = '';
                $ruleRepo->add($base_info);

            } else {
                $cabins = explode('/', $cabin_str);
                if (empty($cabins)) {
                    dd($cabin_str);
                }
                foreach ($cabins as $cabin_code) {
                    $base_info['cabin_code'] = strtoupper($cabin_code);
                    $ruleRepo->add($base_info);
                }
            }

        }
    }
}
